package persistencia;
import entidade.Disciplina;
import java.util.ArrayList;
import java.util.List;

	

	public class DisciplinaDao extends Dao {
		
	    public void cadastrar(Disciplina d)throws Exception{

	        conectar();
	        stmt = con.prepareStatement("insert  into disciplina (codigo, nome) values(?, ?)");
	        stmt.setInt(1, d.getCodigo());
	        stmt.setString(2, d.getNome());
	        stmt.execute();
	        desconectar();
	    }

	    public List<Disciplina> consultaGeral()throws Exception{
	        conectar();
	        List<Disciplina> lista = new ArrayList<Disciplina>();
	        stmt = con.prepareStatement("select * from disciplina");
	        rs = stmt.executeQuery();

	        while(rs.next()){
	            Disciplina d = new Disciplina();
	            d.setId(rs.getInt("id"));
	            d.setCodigo(rs.getInt("codigo"));
	            d.setNome(rs.getString("nome"));
	            lista.add(d);
	        }
	        desconectar();
	        return lista;

	    }


	    public void deletar(int id)throws Exception{
	        conectar();
	        stmt = con.prepareStatement("delete from disciplina where id = ?");
	        stmt.setInt(1, id);
	        stmt.execute();
	        desconectar();

	    }

	    public void editar(Disciplina d)throws Exception{

	        conectar();
	        stmt = con.prepareStatement("update disciplina set codigo =?, nome =? where id =?");
	        stmt.setInt(1, d.getCodigo());
	        stmt.setString(2, d.getNome());
	        stmt.setInt(3, d.getId());
	        stmt.execute();
	        desconectar();

	    }

	    public Disciplina consultarDados(int id)throws Exception{
	        Disciplina d = null;
	        conectar();
	        stmt = con.prepareStatement("select * from disciplina where id =?");
	        stmt.setInt(1, id);
	        rs = stmt.executeQuery();

	        if(rs.next()){
	            d = new Disciplina();
	            d.setId(rs.getInt("id"));
	            d.setCodigo(rs.getInt("codigo"));
	            d.setNome(rs.getString("nome"));
	            
	        }
	      return d;
	    }

	}




